﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace XNAssist
{
    /// <summary>
    /// A simple label that wraps text to a given width
    /// </summary>
    public class TextWrappedLabel
    {
        /// <summary>
        /// The font to draw with
        /// </summary>
        public SpriteFont Font { get; private set; }

        /// <summary>
        /// The text to draw
        /// </summary>
        public string Text { get; set; }

        /// <summary>
        /// The width to constrain the text to
        /// </summary>
        public float TextWidth { get; set; }

        /// <summary>
        /// A simple constructor
        /// </summary>
        /// <param name="font"> The font to draw with </param>
        /// <param name="textWidth"> The width to constrain the text to </param>
        public TextWrappedLabel(SpriteFont font, float textWidth)
        {
            Font = font;
            TextWidth = textWidth;
            Text = String.Empty;
        }

        /// <summary>
        /// A simple constructor
        /// </summary>
        /// <param name="font"> The font to draw with </param>
        /// <param name="textWidth"> The width to constrain the text to </param>
        /// <param name="text"> The text to draw </param>
        public TextWrappedLabel(SpriteFont font, float textWidth, string text)
        {
            Font = font;
            TextWidth = textWidth;
            Text = text;
        }

        /// <summary>
        /// Draws the label
        /// </summary>
        /// <param name="spriteBatch"> The SpriteBatch to draw to </param>
        /// <param name="position"> The position to draw at (based on upper left) </param>
        /// <param name="color"> The color to draw with </param>
        public void Draw(SpriteBatch spriteBatch, Vector2 position, Color color)
        {
            var toDraw = WrapText(Font, TextWidth, Text);

            spriteBatch.DrawString(Font, toDraw, position, color);
        }

        /// <summary>
        /// Takes the given text and wraps it
        /// </summary>
        /// <param name="font"> The font to check sizes with </param>
        /// <param name="textWidth"> The width to constrain the text to </param>
        /// <param name="text"> The text to wrap </param>
        /// <returns> The wrapped text </returns>
        public static string WrapText(SpriteFont font, float textWidth, string text)
        {
            var line = String.Empty;
            var returnString = String.Empty;
            var wordArray = text.Split(' ');

            foreach (var word in wordArray)
            {
                if (font.MeasureString(line + word).Length() > textWidth)
                {
                    returnString = returnString + line + '\n';
                    line = String.Empty;
                }

                line = line + word + ' ';
            }

            return returnString + line;
        }
    }
}
